 

------------------------------------------------------------------------

-- Simple Test for Box2D

------------------------------------------------------------------------
local screenW = 320
local screenH = 480
local dropABox = {}

MOAISim.openWindow ( "Simple Box2D Test", screenW, screenH )

viewport = MOAIViewport.new ()
viewport:setSize ( screenW, screenH )
viewport:setScale ( screenW, screenH )

layer = MOAILayer2D.new ()
layer:setViewport ( viewport )

MOAISim.pushRenderPass ( layer )

boxWorld = MOAIBox2DWorld.new ()
boxWorld:setGravity ( -0, -9 )
boxWorld:setUnitsToMeters ( 1/30, 10 )
boxWorld:start ()

layer:setBox2DWorld ( boxWorld )

ground = boxWorld:addBody ( MOAIBox2DBody.STATIC )
ground:setTransform ( 0, -240 )
groundFix = ground:addRect ( -160, -10, 160, 10 )


num_boxes = 3

------------------------------------------------------------------------
------------------------------------------------------------------------

function callWithDelay ( delay, func,...)
  local timer = MOAITimer.new ()
  timer:setSpan ( delay )
  timer:setListener ( MOAITimer.EVENT_TIMER_LOOP,
    function ()
      --timer:stop ()
      --timer = nil
      func ( unpack ( arg ))
    end
  )
  timer:start ()
end

function dropABox ()
        ball = boxWorld:addBody ( MOAIBox2DBody.DYNAMIC )

        ball:setTransform ( math.random ( -160, 160 ), 200 )

        ballFix = ball:addRect ( - math.random ( 10, 20 ), - math.random ( 10, 30 ),  math.random ( 10, 30 ),  math.random ( 10, 15 ) )
        ballFix.name = "ball"

        ballFix:setFriction ( 0.25)
        ballFix:setDensity ( 1 )
        ballFix:setRestitution ( 0.5 )
        ball:resetMassData()
        if (num_boxes >= 0) then
                num_boxes = num_boxes - 1
                callWithDelay(10000, dropABox())
        end

end


------------------------------------------------------------------------
------------------------------------------------------------------------
--- Handle Input
------------------------------------------------------------------------
------------------------------------------------------------------------
function pointerCallback ( x, y )
        local oldX = mouseX
        local oldY = mouseY
        mouseX, mouseY = layer:wndToWorld ( x, y )
end

function clickCallback ( down )
        if down then
                print ( " down down down " )
                dropABox ()
        end
end

if MOAIInputMgr.device.pointer then
        -- mouse input
        MOAIInputMgr.device.pointer:setCallback ( pointerCallback )
        MOAIInputMgr.device.mouseLeft:setCallback ( clickCallback )
else
        -- touch input
        MOAIInputMgr.device.touch:setCallback (
                function ( eventType, idx, x, y, tapCount )
                        pointerCallback ( x, y )
                        if eventType == MOAITouchSensor.TOUCH_DOWN then
                                clickCallback ( true )
                        elseif eventType == MOAITouchSensor.TOUCH_UP then
                                clickCallback ( false )
                        end
                end
        )
end


callWithDelay(10000, dropABox())
